<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item><a href="/permission/agent">经纪人管理</a></el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="margin: 0 0 20px 0;">
      <el-button type="primary" size="medium"
                 @click="$router.push('/house/agent/add-new')">
        添加经纪人
      </el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="180" align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="gender" label="性别" align="center" width="100px"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="phoneNumber" label="电话号码" align="center"
                       show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="address" label="地址" align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="180" align="center"
                       show-overflow-tooltip>
        <template slot-scope="scope">
          <el-switch
              @change="toggleEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="查看" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="small" @click="agentDetails(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" circle
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="text-align: right; margin: 10px auto;">
      <el-pagination
          @current-change="changePage"
          layout="total, prev, pager, next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize">
      </el-pagination>
    </div>

    <el-dialog title="编辑经纪人" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <div style="width: 30%;">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        </div>
        <div style="width: 30%;">
        <el-form-item label="性别" prop="gender">
          <el-radio v-model="editForm.gender" label="男">男</el-radio>
          <el-radio v-model="editForm.gender" label="女">女</el-radio>
        </el-form-item>
        </div>
        <div style="width: 50%;">
        <el-form-item label="电话" prop="phoneNumber">
          <el-input v-model="editForm.phoneNumber" autocomplete="off"></el-input>
        </el-form-item>
        </div>
        <div style="width: 28%;">
        <el-form-item label="年龄" prop="age">
          <el-input v-model="editForm.age" autocomplete="off"></el-input>
        </el-form-item>
        </div>
        <div style="width: 50%;">
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="editForm.idCard" autocomplete="off"></el-input>
        </el-form-item>
        </div>
        <div style="width: 40%;">
        <el-form-item label="电话号码" prop="phoneNumber">
          <el-input v-model="editForm.phoneNumber" autocomplete="off"></el-input>
        </el-form-item>
        </div>
        <div style="width: 50%;">
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="editForm.address" autocomplete="off"></el-input>
        </el-form-item>
        </div>
        <el-form-item label="从业时间" prop="entryTime">
          <div class="block">
            <el-date-picker
                format="yyyy-MM-dd"
                v-model="editForm.entryTime"
                type="date"
                placeholder="选择日期">
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="查看经纪人详情" :visible.sync="dialogFormVisible2">
      <el-form :model="details" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <div style="width: 30%;">
        <el-form-item label="姓名">
          <el-input v-model="details.name" autocomplete="off" readonly></el-input>
        </el-form-item>
        </div>
        <el-form-item label="性别">
          <el-radio v-model="details.gender" label="男" disabled>男</el-radio>
          <el-radio v-model="details.gender" label="女" disabled>女</el-radio>
        </el-form-item>
        <div style="width: 40%;">
        <el-form-item label="电话">
          <el-input v-model="details.phoneNumber" autocomplete="off" readonly></el-input>
        </el-form-item>
        </div>
        <div style="width: 28%;">
        <el-form-item label="年龄">
          <el-input v-model="details.age" autocomplete="off" readonly></el-input>
        </el-form-item>
        </div>
        <div style="width: 40%;">
        <el-form-item label="身份证">
          <el-input v-model="details.idCard" autocomplete="off" readonly></el-input>
        </el-form-item>
        </div>
        <div style="width: 40%;">
        <el-form-item label="电话号码">
          <el-input v-model="details.phoneNumber" autocomplete="off" readonly></el-input>
        </el-form-item>
        </div>
        <div style="width: 50%;">
        <el-form-item label="家庭住址">
          <el-input v-model="details.address" autocomplete="off" readonly></el-input>
        </el-form-item>
        </div>
        <div style="width: 40%;">
        <el-form-item label="从业时间">
          <el-input v-model="details.entryTime" autocomplete="off" readonly></el-input>
        </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible2 = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roleListOptions: [],
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      dialogFormVisible2: false,
      details: {
        id:'',
        name: '',
        gender:'',
        age:'',
        idCard:'',
        phoneNumber:'',
        address:'',
        entryTime: '',
      },
      editForm: {
        id:'',
        name: '',
        gender:'',
        age:'',
        idCard:'',
        phoneNumber:'',
        address:'',
        entryTime: '',
      },
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        gender: [
          {required: true, message: '请选择性别', trigger: 'blur'}
        ],
        age:[
          {required: true, message: '请输入年龄', trigger: 'blur'},
          {pattern: /^(1[8-9]|[2-5]\d|99)$/,message: '请输入正确年龄',trigger: 'blur'}
        ],
        phoneNumber: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {len: 11,message: '电话号码格式错误',trigger: 'blur'}
        ],
        idCard: [
          {required: true, message: '请输入身份证号', trigger: 'blur'},
          {pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: '身份证格式错误',trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入地址', trigger: 'blur'},
        ],
        entryTime: [
          {required: true, message: '请选择从业时间', trigger: 'blur'}
        ],
        enable: [],

      }
    }
  },
  methods: {
    openEditDialog(agent) {
      let url = 'http://localhost:9200/agent/standard?id=' + agent.id;
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAgentList();
            }
          });
        }
      });
    },
    toggleEnable(agent) {
      let enableText = ['禁用', '启用'];
      let url = 'http://localhost:9200/agent/' + agent.id;
      let enable = agent.enable;
      if (enable == 1) {
        url += '/enable';
      } else {
        url += '/disable';
      }
      console.log('url = ' + url);

      this.axios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '将【' + agent.name + '】的启用状态修改为【' + enableText[enable] + '】成功！',
            type: 'success'
          });
          this.loadAgentList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAgentList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAgentList();
            }
          });
        }
      }).catch(err => {
        let message = '服务器响应错误！';
        this.$alert(message, '操作失败', {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
      });
    },
    submitEditForm(){
      let url = 'http://localhost:9200/agent/update';
      console.log('url = ' + url);


      let formData = this.qs.stringify(this.editForm);

      console.log("formData=" + formData)



      this.axios.post(url,formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state==20000){
          this.$message({
            message: '修改经纪人成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadAgentList();
        } else if (jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAgentList();
            }
          });
        }else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }

      })
    },
    agentDetails(agent) {
      let url = 'http://localhost:9200/agent/standard?id=' + agent.id;
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.dialogFormVisible2 = true;
          this.details = jsonResult.data
          console.log(this.details,'11111111111111')
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openDeleteConfirm(agent) {
      this.$confirm('此操作将永久删除【' + agent.id + '-' + agent.username + '】经纪人，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(agent);
      }).catch(() => {
      });
    },
    handleDelete(agent) {
      let url = 'http://localhost:9200/agent/delete';

      let formData = 'id=' + agent.id;
      this.axios.post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除经纪人成功！',
            type: 'success'
          });
          this.loadAgentList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAgentList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    changePage(value) {
      this.$router.replace('?page=' + value);
      this.loadAgentList();

    },
    loadAgentList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:9200/agent/list?page=' + page;




      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadAgentList();
  }
}
</script>
